<?php
#by reemo 2012
class ShowDmdeffPage extends AbstractPage
{
	public static $requireModule = 0;

	function __construct() 
	{
		parent::__construct();
	}
	
	private function calcTM($Element)
	{
		global $PLANET, $USER, $resource, $pricelist, $CONF;
		
		$metal 		= round(($pricelist[$Element]['cost']['901']) * 0.00025);
		$crystal	= round(($pricelist[$Element]['cost']['902']) * 0.0005);
		$deiterium 	= round(($pricelist[$Element]['cost']['903']) * 0.001);
		$tm	 		= round(($pricelist[$Element]['cost']['921']) * 1);
		
		$nac 		= $CONF['resource_multiplier'] < 50 ? 1.2 : ($CONF['resource_multiplier'] < 500 ? 0.09 : 0.07);
		
		$cost 		= ceil($nac * max(1,($metal + $crystal + $deiterium + $tm)));
	
		return($cost);
	}
	
	public function send()
	{
		global $USER, $PLANET, $LNG, $CONF, $pricelist, $resource, $reslist;
		
		if($CONF['resource_multiplier'] > 500)
				{
					$time_break = TIMESTAMP + 150;
				}
				elseif($CONF['resource_multiplier'] > 50)
					{
						$time_break = TIMESTAMP + 300;
					}
					else
						{
							$time_break = TIMESTAMP + 600;
						}
		
		$ataks	= $GLOBALS['DATABASE']->countquery("SELECT count(*) FROM ".FLEETS." WHERE `fleet_start_time` < ".$time_break." AND `fleet_start_time` > ".TIMESTAMP." AND `fleet_mission` = 1 AND `fleet_owner` <> ".$USER['id']." AND `fleet_end_id` = ".$PLANET['id'].";");
		$unic	= $GLOBALS['DATABASE']->countquery("SELECT count(*) FROM ".FLEETS." WHERE `fleet_start_time` < ".$time_break." AND `fleet_start_time` > ".TIMESTAMP." AND `fleet_mission` = 9 AND `fleet_owner` <> ".$USER['id']." AND `fleet_end_id` = ".$PLANET['id'].";");
		
		
		
		$shipID			= HTTP::_GP('shipID', 0);
		$Count			= max(0, round(HTTP::_GP('count', 0.0)));
		$Elements		= $reslist['defense'];
		$costTM			= $Count * $this->calcTM($shipID);
		if(!empty($shipID) && in_array($shipID, $Elements) && BuildFunctions::isTechnologieAccessible($USER, $PLANET, $shipID) && in_array($shipID, $reslist['defense']) && $shipID != 502 && $shipID != 411 && $shipID != 503 && $shipID != 407 && $shipID != 408 && $shipID != 409 && $unic == 0 && $ataks == 0)
		{
			
			if($USER[$resource[921]] < $costTM )
			{
				$this->printMessage("Недостаточно Темной материи", true, array("game.php?page=dmdeff", 1));
				return;
			}
			
			$USER[$resource[921]]			-= $costTM;
			
			$PLANET[$resource[$shipID]]		+= $Count;
			$GLOBALS['DATABASE']->query("UPDATE ".PLANETS." SET ".$resource[$shipID]." = ".$resource[$shipID]." + ".$Count." WHERE id = ".$PLANET['id'].";");
			$this->printMessage('Оборона куплена', true, array("game.php?page=dmdeff", 1));
		}
		
		$this->redirectTo('game.php?page=dmdeff');
	}
	
	function show()
	{
		global $PLANET, $LNG, $CONF, $pricelist, $resource, $reslist, $USER;
		
		if($CONF['resource_multiplier'] > 500)
				{
					$time_break = TIMESTAMP + 150;
				}
				elseif($CONF['resource_multiplier'] > 50)
					{
						$time_break = TIMESTAMP + 300;
					}
					else
						{
							$time_break = TIMESTAMP + 600;
						}
		
		$ataks	= $GLOBALS['DATABASE']->countquery("SELECT count(*) FROM ".FLEETS." WHERE `fleet_start_time` < ".$time_break." AND `fleet_start_time` > ".TIMESTAMP." AND `fleet_mission` = 1 AND `fleet_owner` <> ".$USER['id']." AND `fleet_end_id` = ".$PLANET['id'].";");
		$unic	= $GLOBALS['DATABASE']->countquery("SELECT count(*) FROM ".FLEETS." WHERE `fleet_start_time` < ".$time_break." AND `fleet_start_time` > ".TIMESTAMP." AND `fleet_mission` = 9 AND `fleet_owner` <> ".$USER['id']." AND `fleet_end_id` = ".$PLANET['id'].";");
			
		if( $ataks > 0 || $unic > 0)
		{
			$this->printMessage("Вблизи Вашей планеты замечен вражеский флот... Связь с торговцем недоступна", true, array("game.php?page=port", 1));
		}
				
		$Elements		= $reslist['defense'];
		$allowedShipIDs = array();
		foreach($Elements as $shipID)
		{
			if(!BuildFunctions::isTechnologieAccessible($USER, $PLANET, $shipID) || !in_array($shipID, $reslist['defense']) || $shipID == 502 || $shipID == 411 || $shipID == 503 || $shipID == 407 || $shipID == 408 || $shipID == 409)
				continue;
			$allowedShipIDs[] = $shipID;
			$Cost[$shipID]	= array($PLANET[$resource[$shipID]], $LNG['tech'][$shipID], $this->calcTM($shipID)) ;
		}
		
		if(empty($Cost)) {
			$this->printMessage("Вы неисследовали ни один флот");
		}
		$this->tplObj->loadscript('dmfleet.js');
		$this->tplObj->assign_vars(array(
			'shipIDs'	=> $allowedShipIDs,
			'CostInfos'	=> $Cost,
			'Charge'	=> $CONF['trade_charge'],
		));
		
		$this->display('page.dm.deff.tpl');
	}
}
?>